package controller

import (
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// GetRoleList
// @Tags SysRole
// @Summary 查询权限列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.QueryRole true " "
// @success 200 {object} response.Response{data=[]model.SysRole} "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /role/getRoleList [get]
func GetRoleList(c *gin.Context) {
	var pageInfo request.QueryRole
	_ = c.ShouldBindQuery(&pageInfo)

	if err := utils.Verify(pageInfo); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := service.GetRoleInfoList(pageInfo)
	pagination := response.Pagination{PageNum: pageInfo.PageNum, PageSize: pageInfo.PageSize, Total: total}
	response.RespondList(list, pagination, err, c)
}

// CreateRole
// @Tags SysRole
// @Summary 新增权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysRole true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /role/addRole [post]
func CreateRole(c *gin.Context) {
	var role model.SysRole
	_ = c.ShouldBindJSON(&role)
	if err := utils.Verify(role); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	role.CreateTime = time.Now()
	role.UpdateTime = time.Now()
	role.CreateBy = user.Username
	role.UpdateBy = user.Username

	err := service.CreateRole(role)
	response.RespondOperate(err, c)
}

// UpdateRole
// @Tags SysRole
// @Summary 更新权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysRole true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /role/updateRole [put]
func UpdateRole(c *gin.Context) {
	var role model.SysRole
	_ = c.ShouldBindJSON(&role)
	if err := utils.Verify(role); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	role.UpdateTime = time.Now()
	role.UpdateBy = user.Username

	err := service.UpdateRole(role)
	response.RespondOperate(err, c)
}

// QueryRole
// @Tags SysRole
// @Summary 查询权限信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id path int true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /role/{id} [get]
func QueryRole(c *gin.Context) {
	roleId, _ := strconv.Atoi(c.Param("id"))
	if roleId == 0 {
		response.FailWithMessage("id不能为空", c)
	}

	role, err := service.QueryRole(uint64(roleId))
	response.RespondData(role, err, c)
}

// DeleteRole
// @Tags SysRole
// @Summary 批量删除权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /role/deleteRole [delete]
func DeleteRole(c *gin.Context) {
	var reqIds request.IdsReq
	_ = c.ShouldBindJSON(&reqIds)
	if err := utils.Verify(reqIds); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := service.DeleteRole(reqIds.Ids)
	response.RespondOperate(err, c)
}
